package com.pig4cloud.pigx.robot.ws.collect.mexc;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.pig4cloud.pigx.robot.ws.collect.FetchManager;
import com.pig4cloud.pigx.robot.ws.collect.bitmex.WsManager;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class ParseData {

	public static void parse(String msg){
		try{
			JSONObject json = JSONObject.parseObject(msg);
			if(json!= null){
				if(json.get("channel").equals("push.tickers")){

					JSONArray datalist = json.getJSONArray("data");
					for(int i = 0 ; i<datalist.size();i++){
						JSONObject item = datalist.getJSONObject(i);

						String symbol = item.getString("symbol").toUpperCase().replace("_","");

						WsManager.getInstance().fetchManager.robotConfigList.forEach(rconfig->{
							if(rconfig.getSymbolName().equals(symbol)){
								//标记价格
								String k_mark = "robot_mexc_markprice_" + symbol;

								String v_mark =item.getString("fairPrice") + "_" + item.getString("timestamp");

								WsManager.getInstance().fetchManager.setdata(k_mark,v_mark);

								//最新成交价格
								String k_last = "robot_mexc_lastprice_"  +  symbol;

								String v_last = item.getString("lastPrice") + "_" + item.getString("timestamp");

								WsManager.getInstance().fetchManager.setdata(k_last,v_last);


								//指数价格
								String k_index = "robot_mexc_indexprice_" +   symbol;

								String v_index = item.getString("indexPrice") + "_" + item.getString("timestamp");

								WsManager.getInstance().fetchManager.setdata(k_index,v_index);
							}
						});




					}



					/******
					 * 单个订阅
					 */


//					//标记价格
//					String k_mark = "robot_mexc_markprice_" + json.getJSONObject("data").getString("symbol").toUpperCase().replace("_","");
//
//					String v_mark = json.getJSONObject("data").getString("fairPrice") + "_" + json.getString("ts");
//
//					WsManager.getInstance().fetchManager.setdata(k_mark,v_mark);
//
//
//
//					//最新成交价格
//					String k_last = "robot_mexc_lastprice_"  + json.getJSONObject("data").getString("symbol").toUpperCase().replace("_","");
//
//					String v_last = json.getJSONObject("data").getString("lastPrice") + "_" + json.getString("ts");
//
//					WsManager.getInstance().fetchManager.setdata(k_last,v_last);
//
//
//					//指数价格
//					String k_index = "robot_mexc_indexprice_" +  json.getJSONObject("data").getString("symbol").toUpperCase().replace("_","");
//
//					String v_index = json.getJSONObject("data").getString("indexPrice") + "_" + json.getString("ts");
//
//					WsManager.getInstance().fetchManager.setdata(k_index,v_index);
				}
			}

		}catch (Exception ex){
			log.error("parse mexc data error  "+ex.getMessage());
		}
	}

}
